package com.qf.dao.impl;

import com.qf.dao.AccountDao;
import com.qf.model.Card;
import com.qf.model.User;
import com.qf.model.pojo.UserCard;
import com.qf.utils.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.Connection;
import java.util.List;

/**
 * @program: bank-master
 * @description:
 * @author: mrsheng
 * @create: 2022-10-09 23:25
 **/


public class AccountDaoImpl extends BaseDao implements AccountDao {

    private JdbcTemplate jdbcTemplate=new JdbcTemplate(DruidUtil.getDataSource());
    QueryRunner runner = new QueryRunner();

    @Override
    public UserCard findCardById(String c_id) {
        String sql="select * from card c join user u on c.u_id=u.u_id where c.c_id=?";
        List<UserCard> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(UserCard.class), c_id);
        if (query.size()>0){
            return query.get(0);
        }
        return null;
    }



    //根据用户ID查找
    @Override
    public List<UserCard> findUid(String u_id) {
        String sql="select * from  card c join user u on c.u_id=u.u_id where u.u_id=? and c_check=1";
        List<UserCard> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(UserCard.class), u_id);
        return query;
    }

    //根据银行卡号 查询已审核银行卡信息
    @Override
    public Card findCardByCid(String cid) {
        String sql="select * from  card where c_check=1 and c_type=0  and c_id=? ";
        return queryForOne(Card.class,sql,cid);
    }

    //修改转入、转出信息
    @Override
    public int updateMoney(String c_id, int a, Double transferMoney) {
        String sql="";
        if(a==1){
            //转出
             sql="update card set c_balance=c_balance-? where c_id=?";
             return update(sql,transferMoney,c_id);
        }else {
            //转入
            sql="update card set c_balance=c_balance+? where c_id=?";
            return update(sql,transferMoney,c_id);
        }
    }

    //修改转入、转出信息
    @Override
    public int transfer(Connection connection,String cid1, String cid2, Double transferMoney) {
        try {

            String sql1="update card set c_balance=c_balance-? where c_id=?";
            String sql2="update card set c_balance=c_balance+? where c_id=?";

            int a=runner.update(connection,sql1,transferMoney,cid1);      //转出
            int b = runner.update(connection, sql2, transferMoney, cid2); //转入
            //如果a，b都等于1，就说明转入、转出sql执行成功
            if (a==1&&b==1){
                return 1;
            }else {
                return 0;
            }
        } catch (Exception e) {
            return 0;
        }
    }


    //根据用户Id查找
    @Override
    public User findUserId(String u_id) {
        String sql="select * from user where u_id=? ";
        return queryForOne(User.class,sql,u_id);
    }


    //提现
    @Override
    public int getMoney(Connection connection,String cid, User user, Double outMoney) {
        try {

            String sql1="update card set c_balance=c_balance+? where c_id=?";
            String sql2="update user set u_balance=u_balance-? where u_id=?";

            int a=runner.update(connection,sql1,outMoney,cid);      //提现到银行卡
            int b = runner.update(connection, sql2, outMoney, user.getU_id()); //aap余额扣款
            //如果a，b都等于1，就说明提现sql执行成功
            if (a==1&&b==1){
                return 1;
            }else {
                return 0;
            }
        } catch (Exception e) {
            return 0;
        }
    }


    //充值
    @Override
    public int TopUp(Connection connection, String cid, User user, Double f) {
        try {

            String sql1="update card set c_balance=c_balance-? where c_id=?";
            String sql2="update user set u_balance=u_balance+? where u_id=?";

            int a=runner.update(connection,sql1,f,cid);      //提现到银行卡
            int b = runner.update(connection, sql2, f, user.getU_id()); //aap余额扣款
            //如果a，b都等于1，就说明提现sql执行成功
            if (a==1&&b==1){
                return 1;
            }else {
                return 0;
            }
        } catch (Exception e) {
            return 0;
        }
    }
}
